<?php

namespace App\Models;

use App\Common\Constant\Params;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class OrderStatus extends BaseModel
{

    protected $table = 'orders_status';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'order_id',
    ];

    protected $casts = [
        'appoint_time' => 'datetime',
        'door_time' => 'datetime',
        'deliver_time' => 'datetime',
        'finish_time' => 'datetime',
        'order_success_time' => 'datetime',
        'deliver_imgs' => 'array',
        'finish_imgs' => 'array',
    ];

    protected $touches = ['order'];

    public function order(){
        return $this->belongsTo(Order::class,'order_id');
    }

    /**
     * 订单交易已取消
     */
    public function getCanceledAttribute()
    {
        return $this->except_status == Params::ORDER_EXCEPT_CANCELED;
    }

    /**
     * 订单提交过退款申请
     */
    public function getHaveRefundAttribute()
    {
        return $this->refund_status != Params::ORDER_REFUND_NORMAL;
    }

    /**
     * 订单提交过仲裁申请
     */
    public function getHaveArbitrateAttribute()
    {
        return $this->arbitrate_status != Params::ORDER_ARBITRATE_NORMAL;
    }

    /**
     * 订单交易已关闭（已退款）
     */
    public function getClosedAttribute()
    {
        return in_array($this->except_status,[Params::ORDER_EXCEPT_REFUNDED,Params::ORDER_EXCEPT_ARBITRATED])
            && ($refund = OrderRefund::where('order_id',$this->order_id)->first())
            && ($refund->refund_type == Params::ORDER_REFUND_FULL)
            && (!$refund->arbitrate_result_type || ($refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_AGREE_FULL));
    }

    /**
     * 订单已退款完成
     */
    public function getRefundedAttribute()
    {
        return $this->except_status == Params::ORDER_EXCEPT_REFUNDED;
    }

    /**
     * 订单已仲裁退款完成
     */
    public function getArbitratedAttribute()
    {
        return $this->except_status == Params::ORDER_EXCEPT_ARBITRATED;
    }

    /**
     * 已雇佣
     */
    public function getEmployedAttribute()
    {
        return ($this->master_status == Params::ORDER_MASTER_STATUS_WAITING)
            || ($this->master_status == Params::ORDER_MASTER_STATUS_SERVING);
    }

    /**
     * 已预约客户上门时间
     */
    public function getAppointedAttribute()
    {
        return $this->master_status == Params::ORDER_MASTER_STATUS_APPOINTED;
    }

    /**
     * 已物流提货
     */
    public function getDeliveredAttribute()
    {
        return ($this->master_status == Params::ORDER_MASTER_STATUS_LOGISTICS_OK)
            || ($this->master_status == Params::ORDER_MASTER_STATUS_LOGISTICS_FAILED);
    }

    /**
     * 已完成服务
     */
    public function getFinishedAttribute()
    {
        return $this->master_status == Params::ORDER_MASTER_STATUS_FINISHED;
    }

    /**
     * 已收款到账
     */
    public function getSuccessAttribute()
    {
        return $this->master_status == Params::ORDER_MASTER_STATUS_SUCCESS;
    }

    /**
     * 预约上门时间文字
     */
    public function getDoorTimeTextAttribute()
    {
        return $this->door_time ? sprintf('%s(%s) %s',
            $this->door_time->format('m月d日'),
            Params::$WEEKS[$this->door_time->format('N')],
            Params::$AMPM[$this->door_time->format('a')]) : null;
    }

    /**
     * 物流提货时间文字
     */
    public function getDeliverTimeTextAttribute()
    {
        return $this->deliver_time ? sprintf('%s(%s) %s',
            $this->deliver_time->format('m月d日'),
            Params::$WEEKS[$this->deliver_time->format('N')],
            Params::$AMPM[$this->deliver_time->format('a')]) : null;
    }

    /**
     * 完成服务时间文字
     */
    public function getFinishTimeTextAttribute()
    {
        return $this->finish_time ? sprintf('%s(%s) %s',
            $this->finish_time->format('m月d日'),
            Params::$WEEKS[$this->finish_time->format('N')],
            Params::$AMPM[$this->finish_time->format('a')]) : null;
    }

    /**
     * 收款到账时间文字
     */
    public function getSuccessTimeTextAttribute()
    {
        return $this->order_success_time ? sprintf('%s(%s) %s',
            $this->order_success_time->format('m月d日'),
            Params::$WEEKS[$this->order_success_time->format('N')],
            Params::$AMPM[$this->order_success_time->format('a')]) : null;
    }

    /**
     * 订单是否已付款
     * paid
     */
//    public function getPaidAttribute()
//    {
//        return in_array($this->merchant_status,[
//            Params::ORDER_MERCHANT_STATUS_PAID,
//            Params::ORDER_MERCHANT_STATUS_SERVING,
//            Params::ORDER_MERCHANT_STATUS_FINISHED,
//            Params::ORDER_MERCHANT_STATUS_SUCCESS,
//        ]);
//    }

    /**
     * 到货状态文本
     * logistics_status_text
     */
    public function getLogisticsStatusTextAttribute()
    {
        return $this->logistics_status ? Params::$LOGISTICS_STATUS[$this->logistics_status] : '- -';
    }

}
